package com.gdth.bank.presenter;

import java.util.List;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import com.gdth.bank.BankApplication;
import com.gdth.bank.event.MessageEvent;
import com.gdth.bank.model.NotificationModel;
import com.gdth.bank.model.SynchronizeModel;
import com.gdth.bank.utils.LogUtils;
import com.gdth.bank.view.NotificationView;
import com.gdth.database.bean.NotificationT;
import com.gdth.widget.dialog.BaseToast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.gdth.utils.GsonUtil;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 通知界面
 * @author Administrator
 *
 */
public class NotificationPresenter {
	
	private NotificationView view;
	private NotificationModel model;
	
	public NotificationPresenter(NotificationView view) {
		this.view = view;
		model = new NotificationModel();
	}
	
	/**
	 *  获取本地所有通知
	 * @param uuid
	 */
	public void  getAllList(final String uuid,final boolean getNet) {		
		model.getAllDataUuid(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
		.subscribe(new Subscriber<List<NotificationT>>(){
			
			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
//				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "上传采集客户信息成功");
			}
			
			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
			}
			
			

			@Override
			public void onNext(List<NotificationT> arg0) {
				// TODO Auto-generated method stub
				if(arg0 != null && arg0.size() > 0) {			// 本地有数据的情况					
					view.getAllNotificationList(arg0);
				}else {		// 本地没有数据的情况
					
				}
				if(getNet) {
					if(arg0 != null && arg0.size() > 0) {
						getNetNotificationList(uuid, String.valueOf(arg0.get(0).getXxtsId()));
					}else {
						getNetNotificationList(uuid, "0");
					}	
				}
		
			}
			
		});

	}
	
	/**
	 *  获取本地未读信息
	 * @param uuid
	 */
	public void  getNoReadList(final String uuid) {		
		model.getNoReadDataUuid(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
		.subscribe(new Subscriber<List<NotificationT>>(){
			
			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
//				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "上传采集客户信息成功");
			}
			
			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
			}
			
			

			@Override
			public void onNext(List<NotificationT> arg0) {
				// TODO Auto-generated method stub
				if(arg0 != null && arg0.size() > 0) {			// 本地有数据的情况					
					view.getAllNotificationList(arg0);
				}else {		// 本地没有数据的情况
					
				}
			}
			
		});

	}
	
	
	/**
	 *  获取本地已读信息
	 * @param uuid
	 */
	public void  getReadList(final String uuid) {		
		model.getReadDataUuid(uuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
		.subscribe(new Subscriber<List<NotificationT>>(){
			
			@Override
			public void onCompleted() {
			}
			
			@Override
			public void onError(Throwable arg0) {
				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
			}
			
			

			@Override
			public void onNext(List<NotificationT> arg0) {
				if(arg0 != null && arg0.size() > 0) {			// 本地有数据的情况					
					view.getAllNotificationList(arg0);
				}else {		// 本地没有数据的情况
					
				}
			}
			
		});

	}
	
	/**
	 *  按标题查找
	 * @param uuid
	 */
	public void  getTitleList(final String uuid,String title) {		
		model.getTitleDataUuid(uuid,title)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
		.subscribe(new Subscriber<List<NotificationT>>(){
			
			@Override
			public void onCompleted() {
			}
			
			@Override
			public void onError(Throwable arg0) {
				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
			}
			
			

			@Override
			public void onNext(List<NotificationT> arg0) {
				if(arg0 != null && arg0.size() > 0) {			// 本地有数据的情况					
					view.getAllNotificationList(arg0);
				}else {		// 本地没有数据的情况
					
				}
			}
			
		});

	}
	
	/**
	 *  按内容查找
	 * @param uuid
	 */
	public void  getContentList(final String uuid,String content) {		
		model.getContentDataUuid(uuid,content)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread()) //AndroidSchedulers.mainThread()
		.subscribe(new Subscriber<List<NotificationT>>(){
			
			@Override
			public void onCompleted() {
			}
			
			@Override
			public void onError(Throwable arg0) {
				BaseToast.getInstance().ShowSystemToast(BankApplication.mInstance.getBaseContext(), "网络连接失败：" + arg0);
			}
			
			

			@Override
			public void onNext(List<NotificationT> arg0) {
				if(arg0 != null && arg0.size() > 0) {			// 本地有数据的情况					
					view.getAllNotificationList(arg0);
				}else {		// 本地没有数据的情况
					
				}
			}
			
		});

	}
	
	
	/**
	*  获取服务器通知数据			
	*/
	public  void getNetNotificationList(String sfz, String lastxxId) {	
		model.NotificationList(sfz,lastxxId)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(Schedulers.io())
		.subscribe(new Subscriber<String>(){
	
			@Override
			public void onCompleted() {
				LogUtils.i("_______","_______");
			}
	
			@Override
			public void onError(Throwable arg0) {
				LogUtils.i("_______","_______" + arg0);
			}
	
			@Override
			public void onNext(String arg0) {		// 数据返回以后， 还要对数据进行解析 -- 并将新数据保存到本地
				//LogUtils.i("_______","_______" + arg0);
				Gson mGson = GsonUtil.getGson();
			     try {
			    	 JSONObject jsonobject = new JSONObject(arg0);
					 String content = jsonobject.getString("data");
					 List<NotificationT> mList =GsonUtil.getGson().fromJson(content, new TypeToken<List<NotificationT>>() {}.getType());
					 if(mList == null || mList.size() == 0)
						 return;
					 SynchronizeModel mSynchronizeModel = new SynchronizeModel();
                	 mSynchronizeModel.saveNotifitionList(mList);
                	 EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_NEWDATA, ""));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	
	/**
	 * 有数据更新
	 * @param uuid
	 */
	public void UpdataList(String uuid) {
		
	}
	
	/**
	 * 删除服务器单条消息
	 * @param sfz
	 * @param lastxxId
	 */
	public void delNotification(final String sfz, final String lastxxId) {
		model.DelNotification(sfz,lastxxId)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(Schedulers.io())
		.subscribe(new Subscriber<String>(){
	
			@Override
			public void onCompleted() {
			
			}
	
			@Override
			public void onError(Throwable arg0) {
				
			}
	
			@Override
			public void onNext(String arg0) {		// 数据返回以后， 还要对数据进行解析 -- 并将新数据保存到本地
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_DEL,lastxxId));
			}
		});
		
	}
	

	/**
	 * 删除服务器多条消息
	 * @param sfz
	 * @param lastxxId
	 */
	public void delMoreNotification(final String sfz,List<Map> delXXIDs) {
//		 Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();  
	
		model.DelMoreNotification(sfz,delXXIDs)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(Schedulers.io())
		.subscribe(new Subscriber<String>(){
	
			@Override
			public void onCompleted() {
				LogUtils.i("_______", "___________onCompleted");
			}
	
			@Override
			public void onError(Throwable arg0) {
				LogUtils.i("_______", "______________onError" + arg0.toString());
			}
	
			@Override
			public void onNext(String arg0) {		// 数据返回以后， 还要对数据进行解析 -- 并将新数据保存到本地
				//LogUtils.i("_______", "______________onNext" + arg0.toString());
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_DEL,""));
			}
		});
		
	}
	

	/**
	 * 删除本地器消息
	 * @param sfz
	 * @param lastxxId
	 */
	public void delLocalNotification(List<NotificationT> id) {
		
		model.delLocalNotification(id)
//		model.DelNotification("","")
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(Schedulers.io())
		.subscribe(new Subscriber<String>(){
	
			@Override
			public void onCompleted() {
			
			}
	
			@Override
			public void onError(Throwable arg0) {
				
			}
	
			@Override
			public void onNext(String arg0) {		// 数据返回以后， 还要对数据进行解析 -- 并将新数据保存到本地
			
			}
		});
		
	}
	
	/**
	 * 修改本地器消息状态
	 * @param sfz
	 * @param lastxxId
	 */
	public void changerLocalNotification(String lastxxId) {
		int id = Integer.parseInt(lastxxId);
		model.changeLocalNotification(id)
//		model.DelNotification("","")
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(Schedulers.io())
		.subscribe(new Subscriber<String>(){
	
			@Override
			public void onCompleted() {
			
			}
	
			@Override
			public void onError(Throwable arg0) {
				
			}
	
			@Override
			public void onNext(String arg0) {		// 数据返回以后， 还要对数据进行解析 -- 并将新数据保存到本地
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_GET, ""));
			}
		});
		
	}
}
